﻿using SuporeSliplampApp.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Newtonsoft.Json;
using System.IO;
using SuporeSliplampApp.Util;

namespace SuporeSliplampApp.Views
{
    /// <summary>
    /// LanguageSetView.xaml 的交互逻辑
    /// </summary>
    public partial class LanguageSetView : Window
    {
        public LanguageSetView()
        {
            InitializeComponent();

            if (Common.languageCode == "zh-CN")
            {
                LanguageComboBox.SelectedIndex = 0;
            }
            else if (Common.languageCode == "en-US")
            {
                LanguageComboBox.SelectedIndex= 1;
            }

        }


        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        //private void SaveBtn_Click(object sender, RoutedEventArgs e)
        //{
        //    var selectedLanguage = (ComboBoxItem)LanguageComboBox.SelectedItem;
        //    string languageCode = selectedLanguage.Tag.ToString();

        //    try
        //    {
        //        switch (languageCode)
        //        {
        //            case ("zh-CN"):
        //                string chinese = "pack://application:,,,/Resource/zh-CN.xaml";
        //                Application.Current.Resources.MergedDictionaries[3].Source = new Uri(chinese);
        //                Common.languageCode = "zh-CN";
        //                return;
        //            case ("en-US"):
        //                string english = "pack://application:,,,/Resource/en-US.xaml";
        //                Application.Current.Resources.MergedDictionaries[3].Source = new Uri(english);
        //                Common.languageCode = "en-US";
        //                return;
        //            default:
        //                return;
        //        }
        //    }
        //    catch (Exception)
        //    {

        //    }
        //    finally
        //    {
        //        var settings = new { LanguageCode = languageCode };
        //        string json = Newtonsoft.Json.JsonConvert.SerializeObject(settings, Newtonsoft.Json.Formatting.Indented);
        //        File.WriteAllText("LanguageSettings.json", json);

        //        this.Close();
        //    }


        //}

        private void SaveBtn_Click2(object sender, RoutedEventArgs e)
        {
            var selectedLanguage = (ComboBoxItem)LanguageComboBox.SelectedItem;
            string languageCode = selectedLanguage.Tag.ToString();

            try
            {
                switch (languageCode)
                {
                    case ("zh-CN"):
                        string chinese = "pack://application:,,,/Resource/zh-CN.xaml";
                        Application.Current.Resources.MergedDictionaries[3].Source = new Uri(chinese);
                        Common.languageCode = "zh-CN";
                        break;
                    case ("en-US"):
                        string english = "pack://application:,,,/Resource/en-US.xaml";
                        Application.Current.Resources.MergedDictionaries[3].Source = new Uri(english);
                        Common.languageCode = "en-US";
                        break;
                    default:
                        string Default = "pack://application:,,,/Resource/zh-CN.xaml";
                        Application.Current.Resources.MergedDictionaries[3].Source = new Uri(Default);
                        Common.languageCode = "zh-CN";
                        break;
                }
                var settings = new { LanguageCode = languageCode };
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(settings, Newtonsoft.Json.Formatting.Indented);
                File.WriteAllText("LanguageSettings.json", json);

                this.Close();

            }
            catch(IOException ex)
            {
                if (Common.languageCode == "zh-CN")
                {
                    MessageBox.Show("数据保存失败");
                }
                else
                {
                    MessageBox.Show("Failed!");
                }
            }
            catch (Exception ex)
            {
                if (Common.languageCode == "zh-CN")
                {
                    MessageBox.Show("数据保存失败");
                }
                else
                {
                    MessageBox.Show("Failed!");
                }
            }
            finally
            {

            }

        }

    }
}
